
package Interface.Producao.Gloss;

import Interface.Producao.DigitaDouble;
import Interface.Producao.DigitaNumeros;
import Interface.Producao.DigitaOS;
import Interface.Producao.JIProducao;
import Interface.Producao.TextDocument;
import java.awt.event.KeyEvent;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import sft.Autenticacao.Observacao;
import sft.Autenticacao.Sessao;
import sft.Logica.Insumos;
import sft.Logica.Producao;
import sft.Logica.ProducaoBusca;
import sft.Principal.Principal;

public class JPGlos extends javax.swing.JPanel{
    
    public JPGlos() {
        initComponents();
        iniciarComponetes();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTOs = new javax.swing.JTextField();
        jCUnid = new javax.swing.JComboBox();
        jCConv = new javax.swing.JComboBox();
        jCnCon = new javax.swing.JComboBox();
        jTValor = new javax.swing.JTextField();
        jTOBS = new javax.swing.JTextField();
        jCStatus = new javax.swing.JComboBox();
        jScrollTabela = new javax.swing.JScrollPane();
        jTTabela = new javax.swing.JTable();
        jScrollTabela = new javax.swing.JScrollPane();    jTTabela.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);  lsmTabela = jTTabela.getSelectionModel();  lsmTabela.addListSelectionListener(new ListSelectionListener() {      public void valueChanged(ListSelectionEvent e) {          if (!e.getValueIsAdjusting()) {              jTTabelaLinhaSelecionada(jTTabela);          }      }  });
        jPanel2 = new javax.swing.JPanel();
        jBAdicionar = new javax.swing.JButton();
        jBEditar = new javax.swing.JButton();
        jBExcluir = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(950, 450));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setMinimumSize(new java.awt.Dimension(909, 100));
        jPanel1.setName(""); // NOI18N

        jLabel2.setText("Unidade");

        jLabel3.setText("Não Conformidade");

        jLabel4.setText("Convenio");

        jLabel5.setText("OS");

        jLabel6.setText("Valor");

        jLabel1.setText("Obs:");

        jLabel7.setText("Situação");

        jTOs.setMinimumSize(new java.awt.Dimension(129, 25));
        jTOs.setPreferredSize(new java.awt.Dimension(129, 25));
        jTOs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTOsActionPerformed(evt);
            }
        });
        jTOs.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTOsFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTOsFocusLost(evt);
            }
        });

        jCUnid.setModel(new DefaultComboBoxModel());
        jCUnid.setMinimumSize(new java.awt.Dimension(169, 25));
        jCUnid.setPreferredSize(new java.awt.Dimension(169, 25));
        jCUnid.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jCUnidKeyPressed(evt);
            }
        });

        jCConv.setEditable(true);
        jCConv.setModel(new DefaultComboBoxModel());
        jCConv.setMinimumSize(new java.awt.Dimension(169, 25));
        jCConv.setPreferredSize(new java.awt.Dimension(169, 25));
        jCConv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCConvActionPerformed(evt);
            }
        });
        jCConv.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jCConvKeyPressed(evt);
            }
        });

        jCnCon.setModel(new DefaultComboBoxModel());
        jCnCon.setMinimumSize(new java.awt.Dimension(246, 25));
        jCnCon.setPreferredSize(new java.awt.Dimension(246, 25));
        jCnCon.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jCnConKeyPressed(evt);
            }
        });

        jTValor.setMinimumSize(new java.awt.Dimension(169, 25));
        jTValor.setPreferredSize(new java.awt.Dimension(169, 25));
        jTValor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTValorActionPerformed(evt);
            }
        });

        jTOBS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTOBSActionPerformed(evt);
            }
        });

        jCStatus.setModel(dmCombo);
        jCStatus.setMinimumSize(new java.awt.Dimension(169, 25));
        jCStatus.setPreferredSize(new java.awt.Dimension(169, 25));
        jCStatus.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jCStatusKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jTOs, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel2)
                            .addComponent(jCUnid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel4)
                            .addComponent(jCConv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTOBS)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jCnCon, 0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jTValor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel6))
                        .addContainerGap(20, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jCConv, jCUnid});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jLabel3)
                        .addComponent(jLabel6)))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTOs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCUnid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCConv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCnCon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTOBS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jCStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addContainerGap())
        );

        jTTabela.setModel(tmTabela);
        jScrollTabela.setViewportView(jTTabela);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jBAdicionar.setText("Adicionar");
        jBAdicionar.setToolTipText("");
        jBAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAdicionarActionPerformed(evt);
            }
        });

        jBEditar.setText("Editar");
        jBEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEditarActionPerformed(evt);
            }
        });

        jBExcluir.setText("Excluir");
        jBExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBExcluirActionPerformed(evt);
            }
        });

        jButton1.setText("Atualizar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jBAdicionar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBEditar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBExcluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jBAdicionar, jBEditar, jBExcluir});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBAdicionar)
                    .addComponent(jBEditar)
                    .addComponent(jBExcluir)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollTabela)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollTabela, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAdicionarActionPerformed
        try {
                adicionar();
                iniciarComponetes();
        } catch (SQLException ex) {
            Logger.getLogger(JPGlos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBAdicionarActionPerformed

    private void jTOsFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTOsFocusLost
        
        try {
            StringBuilder sb = new StringBuilder(jTOs.getText());
            sb.insert(3, "-");
            sb.insert(9, "-");
            jTOs.setDocument(new DigitaOS());
            jTOs.setText(sb.toString());
            setPosto(Integer.parseInt(sb.delete(3, sb.length()).toString()));
        } catch (Exception e) {
            if(!jTOs.getText().equals("")){
                JOptionPane.showMessageDialog(null, "OS Invalida!");
                jTOs.setText("");
            }else{
                jCUnid.setSelectedItem(null);
            }
        }
        
    }//GEN-LAST:event_jTOsFocusLost

    private void jTOsFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTOsFocusGained
        // TODO add your handling code here:
        jTOs.setDocument(new DigitaNumeros());
    }//GEN-LAST:event_jTOsFocusGained

    private void jBEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEditarActionPerformed
        try {
            
            if (JOptionPane.showConfirmDialog(null , "DESEJA EDITAR ESTA OS ?", "EDITAR" ,JOptionPane.OK_OPTION) == 0) {
                editar();
                iniciarComponetes();
            }
        } catch (SQLException ex) {
            Logger.getLogger(JPGlos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBEditarActionPerformed

    private void jBExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBExcluirActionPerformed
        try {
            if(JOptionPane.showConfirmDialog(null, "SE PROCEGUIR ESTA INCONSISTENCIA SERA PERDIDA PERMANENTEMENTE. \n "
                    + "DESEJA PROCEGUIR ?", "EXCLUIR", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == 0){
                excluir();
                iniciarComponetes();
            }
        } catch (SQLException ex) {
            Logger.getLogger(JPGlos.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_jBExcluirActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        iniciarComponetes();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTOsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTOsActionPerformed
        // TODO add your handling code here:
        jCUnid.requestFocus();
    }//GEN-LAST:event_jTOsActionPerformed

    private void jTValorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTValorActionPerformed
        // TODO add your handling code here:
        jTOBS.requestFocus();
    }//GEN-LAST:event_jTValorActionPerformed

    private void jTOBSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTOBSActionPerformed
        // TODO add your handling code here:
        jCStatus.requestFocus();
    }//GEN-LAST:event_jTOBSActionPerformed

    private void jCUnidKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jCUnidKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            jCConv.requestFocus();
        }
        
    }//GEN-LAST:event_jCUnidKeyPressed

    private void jCConvKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jCConvKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            jCnCon.requestFocus();
        }
    }//GEN-LAST:event_jCConvKeyPressed

    private void jCnConKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jCnConKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            jTValor.requestFocus();
        }
    }//GEN-LAST:event_jCnConKeyPressed

    private void jCStatusKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jCStatusKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            jBAdicionar.requestFocus();
        }
    }//GEN-LAST:event_jCStatusKeyPressed

    private void jCConvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCConvActionPerformed
        // TODO add your handling code here:
        try {
            int numConv = Integer.parseInt(jCConv.getSelectedItem().toString());
            for (int i = 0; i < convenios.size(); i++) {
                if (numConv == convenios.get(i).getCod()) {
                    System.out.println(convenios.get(i).getCod());
                    for (int j = 0; j < conv.size(); j++) {
                        if(conv.get(j).getId() == convenios.get(i).getFk_conv()){
                            jCConv.setSelectedItem(conv.get(j).getNome());
                        }                        
                    }
                }
            }
        } catch (Exception e) {
        }
        
    }//GEN-LAST:event_jCConvActionPerformed

        
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAdicionar;
    private javax.swing.JButton jBEditar;
    private javax.swing.JButton jBExcluir;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCConv;
    private javax.swing.JComboBox jCStatus;
    private javax.swing.JComboBox jCUnid;
    private javax.swing.JComboBox jCnCon;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollTabela;
    private javax.swing.JTextField jTOBS;
    private javax.swing.JTextField jTOs;
    private javax.swing.JTable jTTabela;
    private javax.swing.JTextField jTValor;
    // End of variables declaration//GEN-END:variables
    
    //DECLARACAO DE VARIAVEL
    DefaultTableModel tmTabela = new DefaultTableModel(null, new String[]
    {"Numero OS","Usuario", "Unidade","Convenio","Não Conformidade","Observação", "Data", "Valor","Status"});
    DefaultComboBoxModel dmCombo = new DefaultComboBoxModel(new String[]{" ","Perda","Recursado","Devolvido U.N."});
    ListSelectionModel lsmTabela;
    public static List<ProducaoBusca> list;
    public static List vetor = new ArrayList<>();
    public static int ID;
    public static List<Insumos> unid = new ArrayList<>(); // para encontrar Unidade ao digitar a OS.
    public static List<Insumos> convenios = new ArrayList<>();//para encontrar Convenio por codigo
    public static List<Insumos> conv = new ArrayList<>();//para encontrar Convenio por codigo
    //FINALIZAR DECLARACOES
    
    private void iniciarComponetes() {
            jTTabela.setAutoCreateRowSorter(true);
            unid = Principal.getUnid();
            conv = Principal.getConv();
            convenios = Principal.getConvenios();
            comboList(jCUnid, unid);
            comboList(jCConv, conv);
            comboList(jCnCon, Principal.getNcon());
            tabelaList();
            jTOs.setDocument(new DigitaNumeros());
            jTValor.setDocument(new DigitaDouble());
            jTOBS.setDocument(new TextDocument(50));
            jBEditar.setEnabled(false);
            jBExcluir.setEnabled(false);
            jTOs.requestFocus();
            JIProducao.limpaTexto();
            
    }
    
    private void comboList(JComboBox box, List<Insumos> lista) {
        vetor.add(lista);
        box.removeAllItems();
        box.addItem(null);
        for (int i = 0; i < lista.size(); i++) {
            box.addItem(lista.get(i).getNome().toUpperCase());
        }
    }
    
    public void tabelaList(){
        try {
            System.out.println("Pesquisando tabela glosa");
            QueryGlos q = new QueryGlos();
            list = q.getList(new java.util.Date());
            mostrarPesquisa();
            
        } catch (SQLException ex) {
            Logger.getLogger(JPGlos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void setList(List<ProducaoBusca> list) {
        JPGlos.list = list;
        mostrarPesquisa();
    }
    
    private void mostrarPesquisa() {
        while (tmTabela.getRowCount() > 0) {            
            tmTabela.removeRow(0);
        }
        //METODO PARA FORMATAR DOUBLE EM MOEDA
        java.text.DecimalFormat df = new java.text.DecimalFormat("R$ ###,###,##0.00");
        if(list.isEmpty()){
            //caso a lista seja vazia preecha aqui o que deve ser feito
        }else{
            String linha[] = new String[]{null,null,null,null,null,null,null,null};
            //"Id","Usuario","Numero OS","Unidade","Convenio","Não Conformidade", "Data", "Valor"})
            for (int i = 0; i < list.size(); i++) {
                tmTabela.addRow(linha);
                tmTabela.setValueAt(list.get(i).getOS(), i, 0);
                tmTabela.setValueAt(list.get(i).getUsua(), i, 1);
                tmTabela.setValueAt(list.get(i).getUnid(), i, 2);
                tmTabela.setValueAt(list.get(i).getConv(), i, 3);
                tmTabela.setValueAt(list.get(i).getnCon(), i, 4);
                tmTabela.setValueAt(list.get(i).getObs(), i, 5);
                SimpleDateFormat formato = new SimpleDateFormat("dd-MM-yyyy");
                tmTabela.setValueAt(formato.format(list.get(i).getData()), i, 6);                
                tmTabela.setValueAt(list.get(i).getValor(), i, 7);
                tmTabela.setValueAt(list.get(i).getStatus(), i, 8);
            }
        }
    }
    
    private void sleep() {
            
            if(list.get(jTTabela.getSelectedRow()).getUnid().toUpperCase().contains("ZZ")){
                comboList(jCUnid, Principal.getUnid());
                jCUnid.addItem(list.get(jTTabela.getSelectedRow()).getUnid());
            }
            if(list.get(jTTabela.getSelectedRow()).getConv().toUpperCase().contains("ZZ")){
                comboList(jCConv, Principal.getConv());
                jCConv.addItem(list.get(jTTabela.getSelectedRow()).getConv());
            }
            if(list.get(jTTabela.getSelectedRow()).getnCon().toUpperCase().contains("ZZ")){
                comboList(jCnCon, Principal.getNcon());
                jCnCon.addItem(list.get(jTTabela.getSelectedRow()).getnCon());
            }
    }
    public void jTTabelaLinhaSelecionada(JTable Tabela) {
        if (jTTabela.getSelectedRow() != -1) {
            jBEditar.setEnabled(true);
            jBExcluir.setEnabled(true);
            jTOs.setDocument(new DigitaOS());
            jTOs.setText(jTTabela.getValueAt(jTTabela.getSelectedRow(), 0).toString());
            jTValor.setText(jTTabela.getValueAt(jTTabela.getSelectedRow(), 7).toString());
            jTOBS.setText(jTTabela.getValueAt(jTTabela.getSelectedRow(), 5).toString());
            sleep();
            jCUnid.setSelectedItem(jTTabela.getValueAt(jTTabela.getSelectedRow(), 2));
            jCConv.setSelectedItem(jTTabela.getValueAt(jTTabela.getSelectedRow(), 3));
            jCnCon.setSelectedItem(jTTabela.getValueAt(jTTabela.getSelectedRow(), 4));
            jCStatus.setSelectedItem(jTTabela.getValueAt(jTTabela.getSelectedRow(), 8));
        }
    }
    
    @SuppressWarnings("empty-statement")
    private boolean obs(){
        if (jTOBS.getText().isEmpty()) {
            return false;
        } else {
            return true;
        }
    }
    
    @SuppressWarnings("empty-statement")
     private boolean testeOS() {
         if(!list.isEmpty())
         for (ProducaoBusca p : list) {
             if(p.getOS().equals(jTOs.getText())){
                 return false;
             }
         };
         return true;
    }
    //AÇOES DOS BOTOES

    private void adicionar() throws SQLException {
        Producao p = setObjeto();
        if (testeOS()) {
            GlosDao dao = new GlosDao();
            dao.Adicionar(p);
            System.out.println("Adicionar");
            Observacao.getInstance().setAcao("Glosa de OS " + jTOs.getText() + " Adicionada!");
            Observacao.getInstance().gravaOBS();
        } else {
            JOptionPane.showMessageDialog(null, "OS JÁ CADASTRADA!", "ERRO", JOptionPane.ERROR_MESSAGE);
        }
    }
    private void editar() throws SQLException {
        Producao p = setObjeto();
        GlosDao dao = new GlosDao();
        dao.Editar(p);
        Observacao.getInstance().setAcao("Glosa de OS " + jTOs.getText() + " Editada!");
        Observacao.getInstance().gravaOBS();
        
    }   
    private void excluir() throws SQLException {
        Producao p = setObjeto();
        p.setOS(list.get(jTTabela.getSelectedRow()).getOS());
        GlosDao dao = new GlosDao();
        dao.Deletar(p);
        Observacao.getInstance().setAcao("Glosa de OS: " + jTOs.getText() + " Excluida!");
    }
    private Producao setObjeto() {
        JComboBox[] box = new JComboBox[]{jCUnid, jCConv, jCnCon};
        Producao p = new Producao();
        
        p.setObs(jTOBS.getText());
        p.setTabela("sft_glos");
        p.setOS(jTOs.getText());
        p.setValor(Double.valueOf(jTValor.getText()));
        java.util.Date data = new java.util.Date();
        Date dat = new Date(data.getTime());
        p.setData(dat);
        p.setUsuario(Sessao.getInstance().getUsuario().getId());
        //seta Status da OS
        p.setStatus(jCStatus.getSelectedItem().toString());
        //Buscando ID
        for (int j = 0; j < box.length; j++) {
            List<Insumos> ListInsumo = (ArrayList)vetor.get(j);
            for (int i = 0; i < ListInsumo.size(); i++) {
                if (box[j].getSelectedItem().equals(ListInsumo.get(i).getNome().toUpperCase())) {
                    if (j == 0) {p.setUnidade(ListInsumo.get(i).getId());}
                    if (j == 1) {p.setConvenio(ListInsumo.get(i).getId());}
                    if (j == 2) {p.setnConformidade(ListInsumo.get(i).getId());}
                }
            }
        }
        return p;
    }
//FIM DOS BOTOES

    private void setPosto(int cod) {
        for (int i = 0; i < unid.size(); i++) {
            if(unid.get(i).getCod() == cod){
                jCUnid.setSelectedIndex(i+1);
            }
        }
    }

    
    


}
